package com.commerce.img.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.toolbox.NetworkImageView;
import com.commerce.img.entity.DataUrl;
import com.commerce.img.MyAppData;
import com.commerce.img.R;
import com.commerce.img.activity.WallpaperActivity;
import com.commerce.img.tools.MyImgLoader;
import com.commerce.img.tools.MyLoadImg;

import java.util.List;

public class MyImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    Context context;
    List<DataUrl.ImgdataBean> list;
    MyAppData myAppData;

    class ItemHolder extends RecyclerView.ViewHolder{
        TextView textView;
        NetworkImageView imageView;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.li_text);
            imageView = itemView.findViewById(R.id.li_image);
        }
    }
    public MyImgAdapter(Context context, List<DataUrl.ImgdataBean> list, MyAppData myAppData) {
        this.context = context;
        this.list = list;
        this.myAppData = myAppData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent, false);
        final ItemHolder itemHolder = new ItemHolder(view);
        itemHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = itemHolder.getAdapterPosition();
                //传入 application
                //第几张图片
                myAppData.setI(i);
                //图片集合
                myAppData.setListClone(list);
                context.startActivity(new Intent(context, WallpaperActivity.class));
            }
        });
        itemHolder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int i = itemHolder.getAdapterPosition();
                //下载提示
                down(i);
                return false;
            }
        });
        return itemHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemHolder castHolder = (ItemHolder) holder;
        castHolder.textView.setText(list.get(position).getImgName());
        MyImgLoader urlLoad = new MyImgLoader(context, castHolder.imageView, list.get(position).getImgecy());
        urlLoad.inBitmap();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    private void down(final int i1) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle("是否下载")
                .setMessage(list.get(i1).getImgName())
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new MyLoadImg().getImage(context,list.get(i1).getImgecy(),list.get(i1).getImgName());
                    }
                })
                .create();
        dialog.show();
    }

}
